<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>String常将算法</title>
</head>
<body>
  
  <script>
    ;(function(doc){

      function reverseString(val){
        return val.split('').reverse().join("");
        // console.log(val.split('').reverse());
      }

      console.log(reverseString("abcd"));
      

      function Stack(){
        this.data = [];
        this.top = 0;
      }
      Stack.prototype = {
        push: function(element){
          this.data[this.top++] = element;
        },
        pop: function(){
          // console.log(len());
          return this.data[--this.top];
        },
        len: function(){
          this.top;
        }
      }

      function reverseString2(val){
        var s = new Stack(),
            arr = val.split(''),
            len = arr.length,
            result = '';
        
        for(var i = 0; i < len; i++){
          
          s.push(val[i]);
        }

        for(var j = 0; j < len; j++){
         result += s.pop();
        }
        
        return result;
      }
      console.log(reverseString2("abcdefg"));


      function reverseString3(str, len, strOut){
        if(len < 0){
          return strOut;
        }
        strOut += str.charAt(len--);
        return reverseString3(str, len, strOut);
      }
      var string = "abcdefg"
      console.log(reverseString3(string, string.length - 1, ''));


      /**
       * 字符串去重
       * 实现原理:
       *  通过object对象的互易性的原理来实现
       * 
      */
      function removerRepeat(val){
        // 创建对象
        var json = {},
            len = val.length,
            strarr = [];
        for(var i = 0; i < len; i++){
          if(!json[val[i]]){
            json[val[i]] = true;
            strarr.push(val[i]);
          }
        }

        return strarr.join("");
      }
      console.log("字符串去重方法一：",removerRepeat("helloJavascript"))
      
      /**
       * 字符串去重方法2
       * 实现原理:
       *  使用Array数组prototype原型上的filter方法来实现
      */
      function removerRepeat2(str){
        return Array.prototype.filter.call(str, function(char, index, arr){
          return arr.indexOf(char) === index;
        }).join('');
      }
      console.log("字符串去重方法二：",removerRepeat2("helloJavascript"))
      

      /**
      *判断对象是array还是object 
      */
      function getData(val){
        if(val instanceof Object){
          return "object";
        }else if(val instanceof Array){
          return "Array";
        }else{
          return "基本类型";
        }
        
        
      }
      console.log(getData({
        a: 1,
        b: 2,
      }));


      var num = [1, 2, 3, 4, 5, 6, 7, 8, 9];
      var temp = num.filter((val)=>{
        if(val % 2 == 0){
          return val;
        };
      })
      console.log(temp);
    })(document)
  </script>
</body>
</html>